/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.mobile.web;

import com.lczyfz.istep.common.utils.BaiduMapUtils;
import com.lczyfz.istep.common.utils.JsonModel;
import com.lczyfz.istep.common.utils.StringUtils;
import com.lczyfz.istep.common.web.BaseController;
import com.lczyfz.istep.modules.mobile.entity.MobileFile;
import com.lczyfz.istep.modules.mobile.entity.MobileMap;
import com.lczyfz.istep.modules.mobile.service.MobileFileService;
import com.lczyfz.istep.modules.mobile.service.MobileMapService;
import com.lczyfz.istep.modules.mobile.utils.MobileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.*;

/**
 * 移动双录文件管理Controller
 *
 * @author dh
 * @version 2017-03-23
 */
@Controller
@RequestMapping(value = "${frontPath}/mobile/mobileFile")
public class MobileFileUpLoadController extends BaseController {

	@Autowired
	private MobileFileService mobileFileService;
	@Autowired
	private MobileMapService mobileMapService;

	@ModelAttribute
	public MobileFile get(@RequestParam(required=false) String id) {
		MobileFile entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = mobileFileService.get(id);
		}
		if (entity == null){
			entity = new MobileFile();
		}
		return entity;
	}
	/**
	 * @param
	 * @return
	 * @author dingh
	 * @date 2017/2/25
	 * @des 移动双录文件上传
	 */


	@ResponseBody
	@RequestMapping(value = "mobileFileUpload")
	public JsonModel mobileFileUpload(HttpServletRequest request,String code) {
		logger.debug("上传客户端文件===");
		logger.debug("上传客户端文件code==="+code);
		MobileUploadUtils mobileUploadUtils=new MobileUploadUtils();
		MobileFile mobileFile=mobileUploadUtils.uploadFile(request);
		JsonModel jsonModel=new JsonModel();
		if(mobileFile!=null&&mobileUploadUtils.isSuccess())
		{
			mobileFile.setCode(code);
			mobileFileService.save(mobileFile);
			jsonModel.setSuccess(mobileUploadUtils.isSuccess());
			jsonModel.setMsg(mobileUploadUtils.getMessage());
			logger.debug("上传客户端文件===" + mobileUploadUtils.getMessage());
		}else
		{
			jsonModel.setSuccess(false);
			jsonModel.setMsg("上传失败!");
		}


		return jsonModel;
	}


	/**
	 * 文件分片上传服务
	 *
	 * @param request
	 * @param code
	 * @param org
	 * @param insurance
	 * @param longitude
	 * @param latitude
	 * @param userName
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "batchUploadFile")
	public JsonModel batchUploadFile(HttpServletRequest request, String code, String org, String insurance, String longitude, String latitude, String userName) throws IOException {
		JsonModel jsonModel = new JsonModel();
		MobileUploadUtils mobileUploadUtils = new MobileUploadUtils();
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, Object> batchUploadFileMap = mobileUploadUtils.batchUploadFile(multipartRequest);
		for (Map.Entry<String, Object> entry : batchUploadFileMap.entrySet()) {
			System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
		}
		int fileCount = Integer.valueOf(request.getParameter("fileCount"));
		int index = Integer.valueOf(request.getParameter("index"));
		int subFileCount = ((List) batchUploadFileMap.get("data")).size();
		if (index < fileCount) {
			System.out.println();
			if (subFileCount < fileCount) {
				jsonModel.setSuccess(true);
				jsonModel.setMsg("上传第" + (index + 1) + "片成功!");
			} else if (subFileCount == fileCount) {

				MobileFile mobileFile = new MobileFile();
				mobileFile.setCode(code);
				mobileFile.setOrg(org);
				mobileFile.setInsurance(insurance);
				mobileFile.setUserName(userName);

				mobileFile.setName(batchUploadFileMap.get("fileName").toString());
				mobileFile.setFormat(batchUploadFileMap.get("suffix").toString());
				mobileFile.setUrl(batchUploadFileMap.get("url").toString());
				mobileFile.setUrlLocal(batchUploadFileMap.get("fileLocalPath").toString());

				mobileFileService.save(mobileFile);

				//TODO:上传坐标保存
				if (StringUtils.isNotBlank(longitude) && StringUtils.isNotBlank(latitude)) {
					//TODO:上传坐标转换
					Map<String, Object> transformMap = BaiduMapUtils.coordinateTransformation(longitude, latitude);
					String status = transformMap.get("status").toString();
					if (StringUtils.isNotBlank(status) && status.equals("0")) {
						String locationX = transformMap.get("locationX").toString();
						String locationY = transformMap.get("locationY").toString();
						MobileMap mobileMap = new MobileMap();
						mobileMap.setFileName(batchUploadFileMap.get("fileName").toString());
						mobileMap.setLongitude(locationX);
						mobileMap.setLatitude(locationY);
						mobileMap.setUser(userName);
						mobileMapService.save(mobileMap);
						System.out.println("坐标保存成功！");
					} else {
						System.out.println("坐标保存失败！");
					}
				}
				jsonModel.setSuccess(true);
				jsonModel.setMsg("文件全部上传成功!");
				logger.debug("上传客户端文件===" + mobileUploadUtils.getMessage());
			}
		} else {
			jsonModel.setSuccess(false);
			jsonModel.setMsg("上传失败!");
			logger.debug("上传客户端文件===" + mobileUploadUtils.getMessage());
		}
		return jsonModel;
	}


	@ResponseBody
	@RequestMapping(value = "getChunk")
	public JsonModel getChunk(String filename) throws IOException {
		JsonModel jsonModel = new JsonModel();
		List<Integer> data = MobileUploadUtils.fileCountCache.get(filename);
		if (data != null) {
			if (data.size() > 0) {
				jsonModel.setStatus("200");
				jsonModel.setSuccess(true);
				jsonModel.setMsg("获取成功!");

			} else {
				data = new LinkedList<>();
				jsonModel.setStatus("404");
				jsonModel.setSuccess(false);
				jsonModel.setMsg("获取失败!");
			}
		} else {
			data = new LinkedList<>();
			jsonModel.setStatus("404");
			jsonModel.setSuccess(false);
			jsonModel.setMsg("获取失败!");
		}
		System.out.println(data);
		jsonModel.setData(data);
		return jsonModel;
	}

	/**
	 * 音频文件上传，推荐系统
	 * @param request
	 * @author hpp
	 * @return
	 */
//	@ResponseBody
//	@RequestMapping(value = "recordFileUpload")
//	public Map<String,Object> recordFileUpload(HttpServletRequest request,String uniId) {
//		Map<String,Object> map=new HashMap<>();
//		MobileUploadUtils mobileUploadUtils=new MobileUploadUtils();
//		if(StringUtils.isNotBlank(uniId)){
//			MobileFile mobileFile=mobileUploadUtils.recordUploadFile(request,uniId);
//			if(mobileFile!=null&&mobileUploadUtils.isSuccess()) {
//				mobileFile.setCode("record");
//				mobileFileService.save(mobileFile);
//				map.put("success", mobileUploadUtils.isSuccess());
//				map.put("msg", mobileUploadUtils.getMessage());
//				map.put("singer", "singer");
//				map.put("songName", "songName");
//			}else {
//				map.put("success", false);
//				map.put("msg", mobileUploadUtils.getMessage());
//			}
//		} else {
//			map.put("success", false);
//			map.put("msg", "没有传入uniId,上传失败!");
//		}
//		return map;
//	}

	/**
	 * 音频文件上传，推荐系统
	 *
	 * @param request
	 * @return
	 * @author hpp
	 */
	@ResponseBody
	@RequestMapping(value = "recordFileUpload")
	public Map<String, Object> recordFileUpload(HttpServletRequest request, String uniId) {
		Map<String, Object> map = new HashMap<>();
		MobileUploadUtils mobileUploadUtils = new MobileUploadUtils();
		if (StringUtils.isNotBlank(uniId)) {


			Map<String, Object> recordSaveFile = mobileUploadUtils.recordSaveFile(request);

			if (recordSaveFile.get("success").equals(true)) {
				map.put("success", true);
				map.put("msg", recordSaveFile.get("msg"));
				map.put("jsonData", recordSaveFile.get("jsonData"));
				map.put("recResult", recordSaveFile.get("recResult"));
			}

		} else {
			map.put("success", false);
			map.put("msg", "没有传入uniId,上传失败!");
		}
		return map;
	}

}